package com.example.delicioushunter.control.databasecontrol;

import java.io.File;
import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.util.Log;

import com.example.delicioushunter.control.supportcontrol.BitmapProcess;
import com.example.delicioushunter.model.database.FoodColumns;
import com.example.delicioushunter.model.object.Food;

public class FoodControl {
	private final static String FOODS_TABLE = "tbl_foods";

	public int addFood(Food food, SQLiteDatabase db) {
		BitmapProcess process = new BitmapProcess();
		ContentValues values = new ContentValues();
		values.put(FoodColumns.BOOK_ID, food.getBook_id());
		values.put(FoodColumns.NAME, food.getName());
		values.put(FoodColumns.THUMB, process.getBytes(food.getThumb()));
		values.put(FoodColumns.SPD_URI, food.getInfo_spd_uri());

		db.insert(FOODS_TABLE, null, values);
		Log.d("Database", "Add fook");
		int currentId = getLastId(db);
		db.close();
		return currentId;
	}

	public void updateFood(Food food, SQLiteDatabase db) {
		BitmapProcess process = new BitmapProcess();
		ContentValues values = new ContentValues();
		values.put(FoodColumns.NAME, food.getName());
		values.put(FoodColumns.THUMB, process.getBytes(food.getThumb()));
		values.put(FoodColumns.SPD_URI, food.getInfo_spd_uri());
		db.update(FOODS_TABLE, values, FoodColumns.ID + " = ?",
				new String[] { String.valueOf(food.get_id()) });
		db.close();
		Log.d("Database", "Update food");
	}

	public ArrayList<Food> getAllFoodsOnBook(int book_id, SQLiteDatabase db) {
		ArrayList<Food> foods = new ArrayList<Food>();
		String selectQuery = "SELECT  * FROM " + FOODS_TABLE + " WHERE "
				+ FoodColumns.BOOK_ID + " = " + book_id;
		Cursor cursor = db.rawQuery(selectQuery, null);
		BitmapProcess process = new BitmapProcess();
		if (cursor.moveToFirst()) {
			do {
				Food food = new Food();
				food.set_id(Integer.parseInt(cursor.getString(0)));
				food.setBook_id(Integer.parseInt(cursor.getString(1)));
				food.setName(cursor.getString(2));
				food.setThumb(process.getImage(cursor.getBlob(3)));

				foods.add(food);
			} while (cursor.moveToNext());
		}
		db.close();
		Log.d("Database", "Get all foods");
		return foods;
	}

	public Food getFood(int _id, SQLiteDatabase db) {
		BitmapProcess process = new BitmapProcess();
		Cursor cursor = db.query(FOODS_TABLE, new String[] { FoodColumns.ID,
				FoodColumns.BOOK_ID, FoodColumns.NAME, FoodColumns.THUMB, FoodColumns.SPD_URI},
				FoodColumns.ID + "=?", new String[] { String.valueOf(_id) },
				null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();

		Food food = new Food();
		food.set_id(Integer.parseInt(cursor.getString(0)));
		food.setBook_id(Integer.parseInt(cursor.getString(1)));
		food.setName(cursor.getString(2));
		food.setThumb(process.getImage(cursor.getBlob(3)));
		food.setInfo_spd_uri(cursor.getString(4));
		Log.d("Database", "get food");
		db.close();
		return food;
	}

	public void deleteFood(int _id, SQLiteDatabase db) {
		db.delete(FOODS_TABLE, FoodColumns.ID + " = ?",
				new String[] { String.valueOf(_id) });
		db.close();
		Log.d("Database", "delete food");
	}
	
	public int getLastId(SQLiteDatabase db) {
		String query = "SELECT ROWID from " + FOODS_TABLE
				+ " order by ROWID DESC limit 1";
		Cursor c = db.rawQuery(query, null);
		int lastId = 0;
		if (c != null && c.moveToFirst()) {
			lastId = (int) c.getLong(0);
		}
		return lastId;
	}
}
